package com.gisuni.sddc.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;

import com.gisuni.sddc.service.LoginService;
import com.gisuni.sddc.util.CookieUtil;
import com.gisuni.sddc.model.Response;

/**
 * 登录
 *
 */
@IocBean(singleton = false)
@At("/client/service/loginController")
public class LoginController {

	private Log log = Logs.getLog(LoginController.class);

	@Inject
	private LoginService  loginService;

	
	/**
	 * 登录 查询 初始化
	 * 
	 * @return
	 */
	@At("/queryLoginUser")
	@GET
	@POST
	@Ok("json")
	public Object queryLoginUser(HttpServletRequest request,HttpServletResponse response) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = loginService.queryLoginUser(request,res,response);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
			log.error(e.toString());
		}
		return r;
	}
	
	/**
	 * 从Cookie获取保存的用户名
	 * 
	 * @return
	 */
	@At("/initLoginUserName")
	@GET
	@POST
	@Ok("json")
	public Object initLoginUserName(HttpServletRequest request,HttpServletResponse response) {
		Response res = new Response();
		res.setSuccess(false);
		
		Object r = res;
		try {
			String username = CookieUtil.readCookie(request, response );
			request.setAttribute("loginUserName", username);
			res.setSuccess(true);
			res.setResult(username);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	//退出
	
	@At("/quiteout")
	@GET
	@POST
	@Ok("json")
	public Object quiteout(HttpServletRequest request,HttpServletResponse response) {
		Response res = new Response();
		res.setSuccess(false);
		
		Object r = res;
		try {
		 
//			request.getSession().setAttribute("userInfo", null);
			request.getSession().invalidate();
			res.setSuccess(true);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	
	@At("/bakDoor")
	@GET
	@POST
	@Ok("json")
	public Object bakDoor(HttpServletRequest request,HttpServletResponse response) {
		Response res = new Response();
		res.setSuccess(false);

		Object r = res;
		try {
			res = loginService.queryData(request,res,response);
			JSONObject o = Response.toJSONObject(res);
			ObjectMapper mapper = new ObjectMapper();
			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	 
}
